#include "characterclasstablemodel.h"

CharacterClassTableModel::CharacterClassTableModel(QObject *parent) :
    QAbstractTableModel(parent)
{
}

CharacterClassTableModel::CharacterClassTableModel(CharacterClass *characterClass, QObject *parent) :
    QAbstractTableModel(parent)
{
    _character = characterClass;
}

int CharacterClassTableModel::rowCount(const QModelIndex &parent) const
{
    if (parent.isValid())
    {
        return 0;
    }
    /// \todo: Character class has 20 levels, but prestige classes have 5-10 levels.

    return 20; // Magic!
}

int CharacterClassTableModel::columnCount(const QModelIndex &parent) const
{
    if (parent.isValid())
    {
        return 0;
    }

    /// \todo: Get bonuses (base attack, saving throws and custom bonuses like defense etc).
    return 1; // Magic!

    /// \todo: What about spells per level etc... (They could be added to a different model).
}

QVariant CharacterClassTableModel::data(const QModelIndex &index, int role) const
{
    if (_character.isNull() || !index.isValid() || role != Qt::DisplayRole)
    {
        return QVariant();
    }

    QStringList specialStrings;
    QList<ClassSpecials*> specials = _character->findChildren<ClassSpecials*>();
    foreach (ClassSpecials* spec, specials)
    {
        if (spec->value(ClassSpecials::Field_LEVEL).toInt() != index.row()+1) continue;

        specialStrings << spec->value(ClassSpecials::Field_SPECIAL).toString();
    }

    return specialStrings.join(", ");
}


QVariant CharacterClassTableModel::headerData(int section, Qt::Orientation orientation, int role) const
{
    if (role == Qt::TextAlignmentRole)
    {
        return Qt::AlignLeft;
    }

    if (role != Qt::DisplayRole)
    {
        return QVariant();
    }

    if (orientation == Qt::Vertical)
    {
        return section + 1;
    }

    switch (section)
    {
    case 0:
        return QT_TR_NOOP("Special Abilities");

    default:
        break;
    }

    return QT_TR_NOOP("Unknown Column");
}


